package com.btl.service.ai.support.input.common.parse.strategy;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Configuration
public class ParseStrategyFactory {

    @Resource
    private ApplicationContext applicationContext;

    private static final Map<FileTypeEnum, ParseStrategy> strategyMap = new ConcurrentHashMap<>();


    @PostConstruct
    void init() {
        Collection<ParseStrategy> values = applicationContext.getBeansOfType(ParseStrategy.class)
                .values();
        for (ParseStrategy value : values) {
            strategyMap.put(value.fileType(), value);
        }
    }

    public static ParseStrategy getStrategy(FileTypeEnum fileTypeEnum) {
        return strategyMap.getOrDefault(fileTypeEnum, strategyMap.get(FileTypeEnum.TEXT));
    }

}
